<?php
/***
 * The MySQL Improved driver extends the Database_Library to provide 
 * interaction with MySQL database
 */
class Core_Driver_MysqlImproved extends Core_Library_Database {
	
	/**
	 * Connection holds MySQLi resource
	 */
	private $connection;
	
	/**
	 * Query to perform
	 */
	private $query;
	
	/**
	 * Result holds data retrieved from server
	 */
	private $result;
	
	/**
	 * create new connetion to database
	 */
	public function connect(){
		require_once './configs/database_configuration.php';
		/**
		 * connection parametrs
		 */
		$host = DATABASE_SERVER;
		$user = DATABASE_USER;
		$password = DATABASE_PASSWORD;
		$database = CURRENT_DATABASE;
		
		/**
		 * your implementation may require these ...
		 */
		$port = NULL;
		$socket = NULL;
		
		/**
		 * create new mysqli connection
		 */
		$this->connection = new mysqli(
				$host, $user, $password, $database, $port, $socket
		);
		try {
			if($this->connection = new mysqli($host, $user, $password, $database, $port, $socket))
				return TRUE;
			else {
				//throw new Exception("Could not connect the mysql database!");
				header("Location:".SITE_ROOT."install/");
			}
		}
		catch(Exception $e){
			echo $e->getMessage();
		}
	}
	
	/**
	 * Break connection to database
	 */
	public function disconnect(){
		
		try {
			/**
			 * clean up connection
			 */
			if($this->connection->close())
				return TRUE;
			else {
				throw new Exception("Could not close this connection!");
			}
		}
		catch(Exception $e){
			echo $e->getMessage();
		}
	}
	
	/**
	 * Prepare query to execute
	 * 
	 * @param $query
	 */
	public function prepare($query){
		
		try {
			if(($query !== NULL) && (trim($query) != "")){
				$this->query = $query;
				return TRUE;
			}
			else {
				throw new Exception("Query could not be empty!");
			}
		}
		catch(Exception $e){
			echo $e->getMessage();
		}
	}
	
	/**
	 * Execute a preapred query
	 */
	public function query(){
		
		try {
			/**
			 * execute prepared query and store in result variable
			 */
			if(isset($this->query)){
				$this->result = $this->connection->query($this->query);
				if(is_null($this->result)){
					throw new Exception("The result of query <strong>".$query."</strong> is null!");
				}
			}
			else {
				throw new Esception("The query is empty!");
			}
		}
		catch(Exception $e){
			echo $e->getMessage();
		}
		return FALSE;
	}
	
	/**
	 * Fetch a row from the query result
	 * 
	 * @param $type
	 */
	public function fetch($type = 'object'){
		if (isset($this->result))
        {
            switch ($type)
            {
                case 'array':
            
                    //fetch a row as array
                    $row = $this->result->fetch_array();
            
                break;
            
                case 'object':
            
                //fall through...
            
                default:
                
                    //fetch a row as object
                    $row = $this->result->fetch_object();    
                    
                break;
            }
            return $row;
        }
    
        return FALSE;
	}
		
	
	public function dataOperations(){
		try {
			if(isset($this->result)){
				return $this->connection->affected_rows;
			}
			else {
				throw new Exception("Data operation failed!");
			}
		}
		catch(Exception $e){
			echo $e->getMessage();
		}
	}
	
	/**
 	* Sanitize data to be used in a query
 	* 
 	* @param $data
 	*/
	public function escape($data)
	{
    	return $this->connection->real_escape_string($data);
	}
}